#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#include<mmsystem.h>
#include"t2y_main.h"
#include"constants.h"
#include"resources.h"
#include"record.h"
#include"play.h"
#include"thread_lib.h"

#define  BLOCK_SIZE 1024

// =====Global Variables ====

DWORD g_client_voice_record_threadId;
DWORD g_client_voice_play_threadId;

// =====Functions============

DWORD WINAPI client_text_thread(LPVOID   lpParam)   
{   
	SOCKET sock=(SOCKET)lpParam;   
	char szBuff[DEFAULT_BUFFER]={0};   
	int ret;   
	while(1)   
	{   
		ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);   
		if   (ret   ==   0) 
		{
			break;
		}
		else  if(ret == SOCKET_ERROR)   
		{   
			printf("recv()  failed:  %d\n",   WSAGetLastError()); 
			closesocket(sock);  
			break;   
		}   
		szBuff[ret]  =  '\0';   
		printf("Server says: %s\n",   szBuff);   
	}   
	
	return   0;   
}  


SOCKET client_connect_to_server(char* server_ip)
{
	WSADATA   wsd;  
	struct    sockaddr_in   server;   
	struct    hostent  *host = NULL;
	SOCKET    sClient = INVALID_SOCKET;

	server.sin_family = AF_INET;   
	server.sin_port = htons(DEFAULT_PORT);   
	server.sin_addr.s_addr = inet_addr(server_ip);   
   
	if (server.sin_addr.s_addr == INADDR_NONE)   
	{   
		print_info(STR_INVALID_IP);
		return INVALID_SOCKET;
	}   
  
	if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)   
	{   
		printf("Failed   to   load   Winsock   library!\n");  
		return INVALID_SOCKET;
	} 
	
	sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);   
	if   (sClient   ==   INVALID_SOCKET)   
	{   
		printf("socket() failed: %d\n", WSAGetLastError());   
		WSACleanup();   
		return INVALID_SOCKET;
	}   
	
	if   (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)   
	{   
		printf("connect() failed: %d\n", WSAGetLastError());   
		closesocket(sClient); 
		WSACleanup();  
		return INVALID_SOCKET;
	}

	printf("Connect to server succeeded!\n");  
	return sClient;
}

void client_startup(char* server_ip)  
{    
	SOCKET    sClient, sClientVoice;   
	HANDLE hTextThread, hVoiceRecordThread, hVoicePlayThread;
	DWORD dwThreadId;	
	char szBuff[DEFAULT_BUFFER]={0};
    int   ret;	

   
	sClient = client_connect_to_server(server_ip);
	
	hTextThread = CreateThread(NULL, 0, client_text_thread, (LPVOID)sClient, 0, &dwThreadId);   
	if(hTextThread   ==   NULL)
	{   
		printf("CreateThread() failed:  %d\n", GetLastError());   
		closesocket(sClient);   
		WSACleanup();   		
		return;   
	}
	
	while(1)
	{
		if(gets(szBuff))
		{
			if( strcmp(szBuff,START_VOICE) == 0 )
			{
				sClientVoice = client_connect_to_server(server_ip);
				hVoiceRecordThread = create_thread(voice_record_thread_runner, sClientVoice, &g_client_voice_record_threadId);
				hVoicePlayThread = create_thread(voice_play_thread_runner, sClientVoice, &g_client_voice_record_threadId);
				continue;
			}
			
			ret = send(sClient, szBuff, strlen(szBuff), 0);   
			if(ret == 0)   
			{
				continue;   
			}
			else if(ret == SOCKET_ERROR)   
			{   
				printf("send() failed:   %d\n",   WSAGetLastError());   
				continue;   
			}   
		//	printf("Send   %d   bytes\n",   ret);   
			memset(szBuff, 0, DEFAULT_BUFFER);
		}
	}
	
	CloseHandle(hTextThread);   
	CloseHandle(hVoiceRecordThread); 
	CloseHandle(hVoicePlayThread); 

	closesocket(sClient);  
	closesocket(sClientVoice);  

	WSACleanup();   

}   
